package io.github.ilkgunel.dao;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.*;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.UpdateOneModel;
import org.bson.Document;
import org.bson.conversions.Bson;
/*import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;*/
/*import org.springframework.boot.test.context.SpringBootTest;*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;



class MongodJointTest {
    private MongoClient mongod;
    private MongoDatabase data;


    public void mongoJoint() {

        mongod = new MongoClient("localhost", 27017);

        data = mongod.getDatabase("runoob");

    }


    public void closeMongo() {

        mongod.close();

    }//单条插入


    public void insertMongo() {

        MongoCollection ab1 = data.getCollection("ab1");

        Document c = new Document();

        c.append("title", "s标题qq");

        c.append("encode", 22.0);

        ab1.insertOne(c);

        System.out.println("数据插入成功");

    }//多条插入


    public void insertManyMongo() {

        MongoCollection ab1 = data.getCollection("ab1");

        Document c0 = new Document();

        c0.append("title", "标题0");

        c0.append("encode", 21.0);

        Document c1 = new Document();

        c1.append("title", "标题1");

        c1.append("encode", 22.0);

        Document c2 = new Document();

        c2.append("title", "标题2");

        c2.append("encode", 23.0);

        List c = Arrays.asList(c0, c1, c2);

        ab1.insertMany(c);

        System.out.println("数据插入成功");

    }//删除


    void delMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        Document c = new Document();

        c.append("title", "a");

        ab1.deleteOne(c);//两种写法//ab1.deleteOne(Filters.eq("title","b"));

    }//修改信息


    void upMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        ab1.updateOne(Filters.eq("title", "标题"), new Document("$set", new Document("encode", 21.0)));

    }//批量插入，修改，(也可删除)


    void sumMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        ab1.bulkWrite(Arrays.asList(new InsertOneModel<>(new Document("title", "p标题1")), new InsertOneModel<>(new Document("title", "p标题2")), new InsertOneModel<>(new Document("title", "p标题3")), new UpdateOneModel<>(new Document("title", "p标题1"), new Document("$set", new Document("title", "p批量修改标题")))

        ));

    }//查询


    void sendMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable documents = ab1.find().limit(3).skip(2);
        for (Object docu : documents) {
            Document doc=(Document)docu;

            System.out.println(doc.get("_id") + " " + doc.get("title") + " " + doc.get("encode"));

        }

    }//排序


    void sendSortMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        Document c = new Document();//c.append("encode",1);

        c.append("encode", -1);

        FindIterable documents = ab1.find().sort(c);
        for (Object docu : documents) {
            Document doc=(Document)docu;
            System.out.println(doc.get("_id") + " " + doc.get("title") + " " + doc.get("encode"));

        }

    }//条件查询


    void conditionMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable d = ab1.find(Filters.lt("encode", 20));//FindIterable d = ab1.find(Filters.lte("encode",20));//FindIterable d = ab1.find(Filters.gt("encode",20));//FindIterable d = ab1.find(Filters.gte("encode",20));//FindIterable d = ab1.find(Filters.ne("encode",20));

        for (Object docu : d) {
            Document doc=(Document)docu;
            System.out.println(doc.get("_id") + " " + doc.get("title") + " " + doc.get("encode"));

        }

    }//条件查询

    void conditionMongod1() {

        MongoCollection ab1 = data.getCollection("ab1");

        Bson where = Filters.and(Filters.gt("encode", 20), Filters.eq("title", "标题"));

        FindIterable d = ab1.find(where);
        for (Object docu : d) {
            Document doc=(Document)docu;

            System.out.println(doc.get("_id") + " " + doc.get("title") + " " + doc.get("encode"));

        }

    }//查询一张表中的数据条数


    void countMongod() {

        MongoCollection collection = data.getCollection("ab1");

        FindIterable ab1 = collection.find();
        int i = 0;
        for (Object docu : ab1) {
            Document doc=(Document)docu;
            i++;

        }

        System.out.println(i);

    }//从第一条数据开是查询出前五条数据


    void first5MongodToTitle() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable skip = ab1.find().limit(5).skip(0);
        for (Object docu : skip) {
            Document doc=(Document)docu;
            System.out.println(doc.get("title"));

        }

    }//查询等于18的数据


    void encodeEq18Mongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable encode = ab1.find(Filters.eq("encode", 18));
        for (Object docu : encode) {
            Document doc=(Document)docu;
            System.out.println(doc.get("title"));

        }

    }//查询大于等于18的数据


    void encodeGte18Mongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable encode = ab1.find(Filters.gte("encode", 18));
        for (Object docu : encode) {
            Document doc=(Document)docu;
            System.out.println(doc.get("title"));

        }

    }//查询18到22之间的数据


    void encodeGte18Between22Mongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable encode = ab1.find(Filters.and(Filters.gt("encode", 18), Filters.lt("encode", 22)));
        for (Object docu : encode) {
            Document doc=(Document)docu;
            System.out.println(doc.get("title"));

        }

    }//根据标题查询价格


    void getencodeTotitleMongod() {

        MongoCollection ab1 = data.getCollection("ab1");

        FindIterable documents = ab1.find(Filters.eq("title", "标题1"));
        for (Object docu : documents) {
            Document document=(Document)docu;
            System.out.println(document.get("encode"));

        }

    }//模糊查询

    void LikeMongod() {

        MongoCollection ab1 = data.getCollection("ab1");//固定查询//Pattern pattern = Pattern.compile("^标题0$", Pattern.CASE_INSENSITIVE);//包含查询//Pattern pattern = Pattern.compile("^.*标.*$", Pattern.CASE_INSENSITIVE);//左固定//Pattern pattern = Pattern.compile("^标题.*$", Pattern.CASE_INSENSITIVE);//右固定

        Pattern pattern = Pattern.compile("^.*标题$", Pattern.CASE_INSENSITIVE);

        BasicDBObject obj = new BasicDBObject();

        obj.put("title", pattern);

        FindIterable documents = ab1.find(obj);
        for (Object docu : documents) {
            Document document=(Document)docu;

            System.out.println((document).get("encode"));

        }

    }//聚合查询


    void groupMongod() {

        MongoCollection collection = data.getCollection("ab1");//对encode的数量进行分组，并且统计出个数

        Document doc = new Document();

        doc.append("_id", "$encode");

        doc.append("count", new Document("$sum", 1));//查找encode字段中数量大于18的

        Document matchDoc = new Document();

        matchDoc.put("_id", new Document("$gt", 18.0));

        Document group = new Document("$group", doc);

        Document match = new Document("$match", matchDoc);//根据encode字段进行降序排序

        Document sort = new Document("$sort", new Document("_id", 1));//将筛选条件添加到文本集合中

        List doclist = new ArrayList();

        doclist.add(group);

        doclist.add(match);

        doclist.add(sort);//聚合查询并输出

        AggregateIterable aggregate = collection.aggregate(doclist);

        MongoCursor iterator = aggregate.iterator();
        while (iterator.hasNext()) {

            Document next = (Document) iterator.next();
            double encode = next.getDouble("_id");
            int count = next.getInteger("count", 0);

            System.out.println(encode + " " + count);

        }

    }//在mongod控制台下执行下面三句命令

/*db.test2.insert({"jobtitle":"java开发工程师","jobnum":"001"},{"jobtitle":"数据库开发工程师","jobtitle":"002"})

db.test2.insert([{"jobtitle":"java工程师","jobnum":"001"},{"jobtitle":"数据库工程师","jobnum":"002"}])

db.test3.insert([{"depart":"开发1部","jobnum":"001"},{"depart":"开发2部","jobnum":"002"}])*/

//三表带筛选条件关联查询 关键字：lookup

    void lookupMongod() {

        MongoCollection test1 = data.getCollection("test1");

        List aggregateList = new ArrayList<>(1);

        aggregateList.add(Aggregates.lookup("test2", "jobnum", "jobnum", "result"));

        aggregateList.add(Aggregates.match(Filters.ne("result", new ArrayList(0))));

        aggregateList.add(Aggregates.lookup("test3", "jobnum", "jobnum", "results"));

        aggregateList.add(Aggregates.match(Filters.ne("results", new ArrayList(0))));

        aggregateList.add(Aggregates.match(Filters.eq("jobnum", "002")));

        AggregateIterable aggregateIterable = test1.aggregate(aggregateList);//对应得到的json结果参考下方

        for (Object docu : aggregateIterable) {

            Document document=(Document)docu;
            Object result = document.get("result");

            String[] split = result.toString().split(",");

            System.out.println(split[1].substring(split[1].indexOf("=") + 1));

            Object results = document.get("results");

            String[] splits = results.toString().split(",");

            System.out.println(splits[1].substring(splits[1].indexOf("=") + 1));

        }

    }//{ "_id" : { "$oid" : "5eba18b5108d9caa7dba842a" },//"title" : "java开发手册",//"author" : "孤尽",//"jobnum" : "001",//"result" : [{//"_id" : { "$oid" : "5eba1aa2108d9caa7dba8430" },//"jobtitle" : "java工程师",//"jobnum" : "001"//}],//"results" : [{//"_id" : { "$oid" : "5eba3333108d9caa7dba8432" },//"depart" : "开发1部",//"jobnum" : "001"//}]//}

}
